package com.cdivtc.shiro.company;

import java.io.*;

public class Demo01 {
        public static void main(String[] args) throws IOException {
            String file1 = "G:\\360downloads\\1.jpg";
            String file2 = "C:\\Users\\1\\Desktop\\2.jpg";
            copyFile(file1,file2);
            System.out.println("复制成功,请看桌面");
        }
        private static void copyFile(String sourcePath,String destPath) throws IOException{
            File sourceFile = new File(sourcePath);
            if(!sourceFile.exists()){
                System.out.println("您输入的文件源不是合法路径！");
                return;
            }else if(sourceFile.isFile()){
                copyDocument(sourcePath,destPath);
            }
            File destFile = new File(destPath);
            if(!destFile.exists()){
                destFile.mkdirs();
            }

            File[] files = sourceFile.listFiles();
            for(File file : files){
                System.out.println(file.getName());
                if(file.isDirectory()){
                    copyFile(sourcePath + "\\" + file.getName(),destPath + "\\" + file.getName());
                }
                if(file.isFile()){
                    copyDocument(sourcePath + "\\" + file.getName(),destPath + "\\" + file.getName());
                    System.out.println("复制完成");
                }
            }

        }
        public static void copyDocument(String sourcePath,String destPath) throws IOException {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourcePath));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
            byte[] buf = new byte[1024];
            int len = 0;
            while((len = bis.read(buf))!=-1){
                bos.write(buf, 0, len);
            }
            bos.close();
            bis.close();
        }
    }

